package geon.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import geon.model.Email;
import geon.model.EmailTipo;

public class DaoEmail implements Dao<Email> {
	
	private DaoGenerico dao;
	private String sql;

	public DaoEmail() {
		dao = new DaoGenerico();
	}

	@Override
	public Integer add(Email o) throws SQLException {
		try {
			dao.begin();
			sql = "Insert Into TbEmail (EmaCodigo, EmaTipo, EmaEndereco) Values (?, ?, ?)";
			o.setCodigo(dao.getNextId("TbEmail", "EmaCodigo"));
			dao.executeCommand(sql, o.getCodigo(), o.getTipo().toString(), o.getEndereco());
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}
		return o.getCodigo();
	}

	@Override
	public void upd(Email o) throws SQLException {
		try {
			dao.begin();
			sql = "Update TbEmail Set EmaTipo = ?, EmaEndereco = ? Where EmaCodigo = ?";
			dao.executeCommand(sql, o.getTipo().toString(), o.getEndereco(), o.getCodigo());
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}
	}

	@Override
	public void del(int codigo) throws SQLException {
		try {
			dao.begin();
			sql = "Delete From TbEmail Where EmaCodigo = ?";
			dao.executeCommand(sql, codigo);
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}	
	}

	@Override
	public Email set(ResultSet rs) throws SQLException {
		Email email = new Email();
		email.setCodigo(rs.getInt("EmaCodigo"));
		email.setTipo(EmailTipo.valueOf(rs.getString("EmaTipo")));
		email.setEndereco(rs.getString("EmaEndereco"));
		return email;
	}

	@Override
	public Email get(int codigo) throws SQLException {
		Email email = null;
		sql = "Select EmaCodigo, EmaTipo, EmaEndereco From TbEmail Where EmaCodigo = ?";
		ResultSet rs = dao.executeQuery(sql, codigo);
		if (rs.next()) {
			email = set(rs);
		}
		rs.close();
		return email;
	}

	@Override
	public Email get(Object param) throws SQLException {
		Email email = null;
		sql = "Select EmaCodigo, EmaTipo, EmaEndereco From TbEmail Where EmaEndereco = ?";
		ResultSet rs = dao.executeQuery(sql, param);
		if (rs.next()) {
			email = set(rs);
		}
		rs.close();
		return email;
	}

	@Override
	public List<Email> list() throws SQLException {
		List<Email> emails = new LinkedList<Email>();
		sql = "Select EmaCodigo, EmaTipo, EmaEndereco From TbEmail";
		ResultSet rs = dao.executeQuery(sql);
		while (rs.next()) {
			emails.add(set(rs));
		}
		rs.close();
		return emails;
	}

	@Override
	public List<Email> list(Object...params) throws SQLException {
		List<Email> emails = new LinkedList<Email>();
		sql = "Select EmaCodigo, EmaTipo, EmaEndereco From TbEmail Where EmaEndereco Like ?";
		ResultSet rs = dao.executeQuery(sql, "%" + params[0] + "%");
		while (rs.next()) {
			emails.add(set(rs));
		}
		rs.close();
		return emails;
	}

}
